/*
 * @lc app=leetcode.cn id=202 lang=typescript
 *
 * [202] 快乐数
 */

// @lc code=start
function isHappy(n: number): boolean {
    let cache = new Set<number>();
    let count: number = n;

    while (count !== 1) {
        count = getSum(count);
        if (cache.has(count)) {
            return false;
        } else {
            cache.add(count);
        }
    }
    return true;
}

function getSum(n: number): number {
    const arr = n.toString().split("");
    return arr.reduce((col, cur) => {
        return col + parseInt(cur) ** 2;
    }, 0);
}
// @lc code=end
